<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* IO Handler
*
* Functions Regarding Input and Output and what to do with Input
* 
* Provides the Crucial Bridge Layer Between "The Game" and the Server 
* Architecture (This is our "Glue")
*/

class ioHandler {
		
	/* Utilities */
	
	public static function inputecho($msg=""){ if(false) { echo $msg."\n"; } }   /* For Testing, but not Debugging */
	
	public static function      wrap($msg=""){ return chr(0).$msg.chr(255); }
	public static function    unwrap($msg=""){ return substr($msg,1,strlen($msg)-2); }
	public static function   console($msg="",$debug){ if($debug){ echo $msg."\n"; } }
	
	/* The Main Command Processor (shovels off to cmdHandler) */
	
	public static function process($connector,$msg){
		$msg = ioHandler::unwrap($msg);
		ioHandler::inputecho("< ".$msg); /* For Testing, but not Debugging */
		
		cmdHandler::splitCmd($connector->user,$msg); /* This Data is Now Part of the Game */
	}
	
	/* Login Functions */
	
	// Both of these Functions Will Eventually Shovel Off the 
	// Welcome task to a dedicated static login Routine in a Separate Class
	public static function entryProcess($connector,$msg) {
			$msg = ioHandler::unwrap($msg);
			$connector->user = new UserCharacter();
			$connector->user->connector = $connector;
			$connector->user->name = $msg;
			$connector->user->description ="some guy";
			$connector->user->image ="genericman1.jpg";
			$connector->loggedin = true;
			$connector->user->position = POS_STANDING;
			world::add_char_to_world($connector->user);
			world::$room_list[0]->add_char_to_room($connector->user);
			$connector->user->in_room = world::$room_list[0];
			$connector->user->loadClient();
	}
	
	public static function onEntry($connector) {
			// We're dealing with a pure connector, so this is manually styled
			$connector->send(strPackager::package("This is where we ask for a name",GENERAL_MSG));
	}
	
	/* Disconnect Functions */
	
	// This comes off the Server Directly
	// In the future we will be able to leave players in the game after disconnect
	// using this function to tell the game they are "socketless"
	// But for now, we keep it simple.
	public static function unexpectedDisconnect($connector) {
			if(!isset($connector->user)) { return; };
			world::rmv_char_from_world($connector->user);
			$connector->user->in_room->rmv_char_from_room($connector->user);
			echo("\n".$connector->user->name." unexpected Disconnect.");
	}
	
	// This is invoked by a game by directive
	public static function doDisconnect($connector) {
		world::rmv_char_from_world($connector->user);
		$connector->user->in_room->rmv_char_from_room($connector->user);
		$connector->server->disconnect($connector->socket); 
	}
}

?>